//
//  RGSearchVedioView.m
//  RedGrass
//
//  Created by wukong on 2019/2/16.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGSearchVedioView.h"
#import "RGSearchVedioCell.h"
#import <MJRefresh.h>

@interface RGSearchVedioView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) MJRefreshAutoNormalFooter *freshFooter;
@property (strong, nonatomic) NSMutableArray *vedioList;

@end

@implementation RGSearchVedioView

-(instancetype) initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
    }
    return self;
}

//UICollectionViewDataSource Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return 10;
    return [_vedioList count];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return  CGSizeMake((int)(KScreenWidth/2), (int)(1.5 * KScreenWidth / 2));
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RGSearchVedioCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RGSearchVedioCell" forIndexPath:indexPath];
    if([_vedioList count]){
        RGRecommendVideoModel *model = _vedioList[indexPath.row];
        if(model){
            cell.model = model;
        }
    }
    return cell;
}

//UICollectionViewDelegate Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if([self.viewDelegate respondsToSelector:@selector(cellItemChosedWithView:chosedIndexPath:)]){
        [self.viewDelegate cellItemChosedWithView:self chosedIndexPath:indexPath];
    }
}


#pragma mark - public

-(void) reloadDataWithVedioList:(NSMutableArray *)vedioList{
    _vedioList = vedioList;
    [_collectionView reloadData];
}



-(void) endFooterFresh{
    [_collectionView.mj_footer endRefreshing];
}

-(void) viewHaveNoMoreData{
    [_collectionView.mj_footer endRefreshingWithNoMoreData];
}



#pragma mark -response

-(void)loadMoreVedio{
    if([self.viewDelegate respondsToSelector:@selector(loadMoreDataWithView:)]){
        [self.viewDelegate loadMoreDataWithView:self];
    }
}


#pragma mark - getter

-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 1;
        flowLayout.minimumLineSpacing = 1;
        _collectionView = [[UICollectionView  alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            //            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[RGSearchVedioCell class] forCellWithReuseIdentifier:@"RGSearchVedioCell"];
        _collectionView.mj_footer = self.freshFooter;
    }
    return _collectionView;
}


-(MJRefreshAutoNormalFooter *)freshFooter{
    if(!_freshFooter){
        _freshFooter = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreVedio)];
        [_freshFooter setTitle:@"暂时没有更多了" forState:MJRefreshStateNoMoreData];
        _freshFooter.automaticallyChangeAlpha = YES;
    }
    return _freshFooter;
}


@end
